import gsApi from "@/api/gsApi"

export default class ProgressBar{
  //弹窗控制符
  initDialogVisible=false
  //控制进度条显示状态，有三个值可选：success/exception/warning
  progressStatus=null
  //进度值
  percentage=0
  IntervalFlag=null
       /**
       * 小车初始化的进度条是假的，根据传入的时间得到
       * time是秒为单位
       */
  async percentageShow(time,webSocket){
    let interval = time/100;
    this.IntervalFlag = setInterval(() => {
      //根据传入的预估时间让假进度条进行到90%
      const status = webSocket.taskStatus.statusCode;
      if(status == 600){
        //正在转圈
        this.progressStatus = null
        this.percentage++;
      }else if(status == 601){
        //不能转圈
        this.progressStatus = 'warning'
      }else if( status == 603 ){
        //转圈失败
        this.percentage=0
        this.progressStatus = 'exception'
      }
      if(this.percentage==90){
        clearInterval(this.IntervalFlag)
        // let key=0;
        const flag = setInterval(async() => {
          //定期发送请求，监听小车是否真的完成初始化了
          const res = await gsApi.checkInitialize();
          // key++;
          if(res.data.data=='true'){
            //一旦收到完成初始化的通知，则1.关闭监听 2.进度条从90到100，3.进度条显示绿色勾 4.关闭该模态化弹窗
            clearInterval(flag);
            const _flag = setInterval(() => {
              this.percentage++
              if(this.percentage==100){
                clearInterval(_flag)
                this.progressStatus = 'success'
              }
            }, 200);
          }      
        }, 500);
      }
    }, interval*1000);
  }
  
  async taskProgress(time,webSocket){
    let interval = time/100
    this.IntervalFlag = setInterval(() => {
      this.percentage++;
    },interval*1000)
    if(this.percentage==90){
      clearInterval(this.IntervalFlag)
    }
  }

  async finishRest(){
    const _flag = setInterval(() => {
      this.percentage++
      if(this.percentage==100){
        clearInterval(_flag)
        this.progressStatus = 'success'
      }
    }, 200);
  }
}